<?php

require_once WACT_ROOT . 'controller/controller.inc.php';
require_once WACT_ROOT. 'util/dataspace.inc.php';
require_once APP_ROOT.'view/pub/layoutright.view.php';

require_once APP_ROOT.'model/common/user.php';
require_once APP_ROOT.'model/common/categories.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/article.php';
require_once APP_ROOT.'model/common/scrolling.php';
require_once APP_ROOT . "model/common/site.php";
require_once APP_ROOT . "model/common/statdivisions.php";
require_once APP_ROOT . "model/common/statteams.php";
require_once APP_ROOT . "model/common/statscores.php";
require_once APP_ROOT . "model/common/parameters.php";


class TeamStatsView extends LayoutRightView {
	function TeamStatsView ($TemplateFile){
		parent::LayoutRightView($TemplateFile);

	}
	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare();

		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
        $this->Template->set('title',Site::getTitle());

		if(!Session::isLogin())
			$this->Template->set('login',' ');

		if(APP_NAME!="") {
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

		// populate team details
		$teamID = 0;
		if ($request->hasParameter('teamID'))
			$teamID = $request->getParameter('teamID');
        if (!$teamID && $request->hasParameter('team1ID'))
			$teamID = $request->getParameter('team1ID');
        if (!$teamID && $request->hasParameter('team2ID'))
			$teamID = $request->getParameter('team2ID');

		//print 'from load: user-id='.$user_id;
     	if ($teamID) {
        	$record =& StatTeam::getRecord($teamID);
        	if (is_null($record)) {
            	return 'notfound';
        	}
        	$divisionName =  $record->get('divisionName');
        	$teamName =  $record->get('teamName');
        	$leagueName =  $record->get('leagueName');
        	if ($leagueName) $divisionName = $leagueName.':'.$divisionName;
        	if ($divisionName) $this->Template->set('divisionName',$divisionName);
        	if ($teamName) $this->Template->set('teamName',$teamName);

            //$responseModel->import($record->export());
            $scores =& Score::getPublicScores($teamID);
            //print_r($scores);
            $this->Template->setChildDataSource('teamstats',$scores);
        }

	$videoShow = Parameters::getVideoStatus();
        if ( $videoShow ) {
			// set video playlist XML file
        	$filename =& Video::getPlaylistFilename(0,0);
        	$this->Template->set('playlist',$filename);
        	Video::savePlaylistFile(0,0);

        	// prevent local caching
        	$rnumber = rand(1, 9999999);
        	$this->Template->set('rnumber',$rnumber);
        }

        // set video playlist XML file
        //$filename =& Video::getPlaylistFilename(0,0);
        //$this->Template->set('playlist',$filename);

        //Video::savePlaylistFile(0,0);

	}
}



class TeamStatsPage extends PageController {

    function TeamStatsPage() {
        parent::PageController();
	$site_template = Site::getPublicTemplatePath('/pub/teamstats.html');

        $this->setDefaultView(new TeamStatsView($site_template));


    }

}

?>
